﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Globalization;

namespace Conversions
{
    /// <summary>
    /// Converts various string in Hex to strings in another base
    /// </summary>
    public class Hex 
    {
       // public string[] validHex = { "A", "B", "C", "D", "E", "F", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" };

        public static string howManyBytes(string _hexString) 
        {
            string lengthInBytes;
            if ((_hexString.Length % 2) != 0)
            {
                lengthInBytes = (_hexString.Length / 2 + 1).ToString();
            }
            else
            {
                lengthInBytes = (_hexString.Length / 2).ToString();
            }

            return lengthInBytes;
        }

        public static string toDecimal(string _hexString)
        {
            if (stringValidation.Number.isNumeric(_hexString, System.Globalization.NumberStyles.HexNumber))
            {
                UInt64 temp = UInt64.Parse(_hexString, NumberStyles.AllowHexSpecifier);
                return temp.ToString();
            }
            else
            {
                return "Contains invalid hex characters";
            }
           
        }

        public static string toBinary(string _hexString)
        {
            if (stringValidation.Number.isNumeric(_hexString, System.Globalization.NumberStyles.HexNumber))
            {
                return Convert.ToString(Convert.ToInt32(_hexString, 16), 2);
            }
            else
            {
                return "Contains invalid hex characters";
            }
        }

        public static string toBinary(string _hexString, int _bitMask)
        {
            if (stringValidation.Number.isNumeric(_hexString, System.Globalization.NumberStyles.HexNumber))
            {

                return Convert.ToString(Convert.ToInt32(_hexString, 16), 2).PadLeft(_bitMask,'0');
                

                //Convert.ToString(Convert.ToInt32(_hexString, 16), 2);
            }
            else
            {
                return "Contains invalid hex characters";
            }
        }
   
    }
    public class Decimal
    {
        public static string howManyBytes(string _decimalString)
        {
            return Hex.howManyBytes(Convert.ToUInt64(_decimalString).ToString("X")); ;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_decimalString"></param>
        /// <returns></returns>
        public static string toHex(string _decimalString)
        {

            if (!stringValidation.Number.isNumeric(_decimalString, System.Globalization.NumberStyles.Integer))
            {

                return "Invalid number in decimal field";
            }
            UInt64 tempNum = Convert.ToUInt64(_decimalString);
            
            return tempNum.ToString("X");
        }
        /// <summary>
        /// Converts a string of decimal numbers to a string of the equavlent hex number and specified number of 
        /// </summary>
        /// <param name="_decimalString">Properly formatted decimal number</param>
        /// <param name="_hexMask">Number of bytes you want ot show, truncates anything less</param>
        /// <returns></returns>
        public static string toHex(string _decimalString, int _hexMask)
        {
            if (!stringValidation.Number.isNumeric(_decimalString, System.Globalization.NumberStyles.Integer))
            {

                return "Invalid number in decimal field";
            }
            string format = "X";
            format = format + _hexMask.ToString();
            UInt64 tempNum = Convert.ToUInt64(_decimalString);
            return tempNum.ToString(format);
            
        }

  

    }
}
